<?php
	/**
	*菜单管理类
	**/
	class MenuClass {
		// 构造函数
		public function __constract(){
		}

		// 菜单列表
		public function getList(){
			global $db;

			// 获取所有数据
			$db->query("select SQL_CALC_FOUND_ROWS
							menu.id,
							menu.name,
							menu.url,
							menu.type,
							menu.parent_id,
							menu.create_time,
							user.name as creator_name,
							menu.flag,
							menu.listorder
						from admin_menu as menu
						left join admin_user as user
						on menu.creator=user.id
						order by menu.listorder asc, menu.id asc", "");

			return $db->prepare->fetchAll();
		}

		// 添加菜单
		public function add($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$name = $data->name;
			$url = $data->url;
			$type = $data->type;
			$parentId = $data->parentId;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询菜单名称是否已经存在
			$db->query("select * 
						from admin_menu
						where
							name=:name
						for update",
						array(
							"name"=>$name
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "菜单名称已经存在了，请重新命名！", null);
			}

			// 只有菜单为启用状态时才能添加子菜单
			if($parentId != 0){
				$db->query("select * 
							from admin_menu
							where
								id=:id
							for update",
							array(
								"id"=>$parentId
								)
							);
				$result = $db->prepare->fetchAll();
				if($result[0]["flag"] == 0){
					returnAjaxJson(400, "只有菜单为启用状态时才能添加子菜单！", null);
				}
			}

			// 插入一条新数据
			$db->query("insert into admin_menu(
							name,
							url,
							type,
							parent_id,
							creator) 
						values(
							:name,
							:url,
							:type,
							:parentId,
							:creator
						)", 
						array(
							"name"=>$name,
							"url"=>$url,
							"type"=>$type,
							"creator"=>$loginUserId,
							"parentId"=>$parentId
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 删除菜单
		public function del($data){
			global $db;
			$id = $data->id;

			// 查询admin_user_role表中的数据
			$db->query("select * from admin_role_menu
						where 
							menuid=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				returnAjaxJson(400, "有角色组绑定了这个菜单，不可以删除！", null);
			}

			// 查询菜单是否含有子菜单
			$db->query("select * 
						from admin_menu
						where
							parent_id=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				returnAjaxJson(400, "菜单含有子菜单，不能删除！", null);
			}

			// 删除admin_menu表中的数据
			$db->query("delete from admin_menu
						where 
							id=:id",
						array(
							"id"=>$id
							)
						);
		}

		// 编辑菜单
		public function edit($data){
			global $db;
			$id = $data->id;
			$name = $data->name;
			$url = $data->url;
			$type = $data->type;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询菜单名称是否已经存在
			$db->query("select * 
						from admin_menu
						where
							name=:name and
							id!=:id
						for update",
						array(
							"name"=>$name,
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "菜单名称已经存在了，请重新命名！", null);
			}

			// 查看type值为0，type不能修改,type不为0时，$type不能为0
			$db->query("select type
						from admin_menu
						where
							id=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result[0]["type"]==0){
				if($type != 0){
					$db->PDO->rollBack();
					returnAjaxJson(400, "菜单类型为菜单，不能修改该菜单类型！", null);
				}
			} else{
				if($type == 0){
					$db->PDO->rollBack();
					returnAjaxJson(400, "菜单类型为子菜单或者隐藏菜单，不能修改为菜单类型！", null);
				}
			}

			// 更新数据
			$db->query("update admin_menu
						set
							name=:name,
							url=:url,
							type=:type
						where
							id=:id",
						array(
							"id"=>$id,
							"name"=>$name,
							"url"=>$url,
							"type"=>$type
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 启用、禁用用户
		public function enable($data){
			global $db;
			$id = $data->id;
			$flag = $data->flag;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 禁用菜单
			if($flag == 0){
				// 查询菜单是否含有启用的子菜单
				$db->query("select * 
							from admin_menu
							where
								parent_id=:id and
								flag=1
							for update",
							array(
								"id"=>$id
								)
							);
				$result = $db->prepare->fetchAll();
				if($result){
					$db->PDO->rollBack();
					returnAjaxJson(400, "菜单含有启用的子菜单，不能禁用！", null);
				}
			} else if($flag == 1){

				// 递归启用菜单父菜单
				function recursion($id){
					global $db;

					// 查询菜单是否含有启用的子菜单
					$db->query("select parent_id
								from admin_menu
								where
									id=:id
								for update"	
								,
								array(
									"id"=>$id
									)
								);
					$result = $db->prepare->fetchAll();
					$parentId = $result[0]["parent_id"];
					if($parentId != 0){
						// 更新数据
						$db->query("update admin_menu 
									set 
										flag=:flag
									where
										id=:id
									",
									array(
										"flag"=>1,
										"id"=>$parentId
										)
									);
						recursion($parentId);
					}
				}

				recursion($id);
			} else {
				returnAjaxJson(400, "参数有误！", null);
			}

			// 更新数据
			$db->query("update admin_menu 
						set 
							flag=:flag
						where
							id=:id
						",
						array(
							"flag"=>$flag,
							"id"=>$id
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 排序
		public function order($data){
			global $db;
			$orderArr = $data;
			$len = count($orderArr);

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			for($i=0; $i<$len; $i++){
				$item = $orderArr[$i];
				$listOrder = $item->listorder;
				$id = $item->id;
				// 更新数据
				$db->query("update admin_menu
							set
								listorder=:listorder
							where
								id=:id",
							array(
								"id"=>$id,
								"listorder"=>$listOrder
								)
							);
				$result = $db->prepare->rowCount();
				if(!$result){
					break;
					$db->PDO->rollBack();
					returnAjaxJson(400, "操作失败！", null);
				}
			}

			$db->PDO->commit();
			// 事物结束
		}
	}
?>